import loadScript from './loadScript'
import ELEMENT from 'element-ui'

let html5QrcodeObj

export default function loadHtml5Qrcode(cb) {
  const html5QrcodeUrl = window.origin + '/plugins/html5-qrcode-2.3.7.min.js'

  if (html5QrcodeObj) {
    cb(html5QrcodeObj)
    return
  }

  const loading = ELEMENT.Loading.service({
    fullscreen: true,
    lock: true,
    text: '加载扫码插件中...',
    spinner: 'el-icon-loading',
    background: 'rgba(255, 255, 255, 0.5)'
  })

  loadScript(html5QrcodeUrl, () => {
    loading.close()
    // eslint-disable-next-line no-undef
    html5QrcodeObj = Html5Qrcode
    cb(html5QrcodeObj)
  })
}
